//
//  PartyParser.m
//  Bespoke
//
//  Created by Cheng on 12/7/6.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "GDataXMLNode.h"
#import "MediaParser.h"
#import "MediaLists.h"
#import "MediaListsText.h"
#import "MediaListsMedia.h"
#import "MediaListsVideo.h"
#import "MediaListsNote.h"
#import "Split.h"

@implementation MediaParser

+ (NSMutableArray *)loadMediaLists:(NSData *)data {
    
    NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    
    GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithXMLString:result options:0 error:nil];
    if (doc == nil) { return nil; }
    
    
    //Create the MediaLists Master Array Container
    NSMutableArray *MedialistsArray = [[NSMutableArray alloc] init];
    
    
    //Get the Media Memebers from the XML
    NSArray *MediaListArray_doc = [doc nodesForXPath:@"//MediaLists" error:nil];

    //Loop into each Media Memeber
    
    NSArray *MediaArray = [[MediaListArray_doc objectAtIndex:0]elementsForName:@"Media"];
    MediaLists *MediaListsOB = [[MediaLists alloc]init];
    
    for (GDataXMLElement * mediaElement in MediaArray)
    {
        //Create and initialize one MediaOB
        MediaListsMedia *MediaOB = [[MediaListsMedia alloc] init];
        
        //Initialize the Media comopnent
        int MediaID = [[[mediaElement attributeForName:@"ID"] stringValue] intValue];
        NSString *NS_BeMediaID = [[mediaElement attributeForName:@"BeMediaID"] stringValue];
        NSString *MediaType = [[mediaElement attributeForName:@"MediaType"] stringValue];
        NSString *WebLink = [[[mediaElement attributeForName:@"WebLink"] stringValue] StringParseSymbol];
        NSString *NS_Source = [[mediaElement attributeForName:@"Source"] stringValue];
        NSString *NS_Author = [[mediaElement attributeForName:@"Author"] stringValue];
        NSString *NS_lastUpdate = [[mediaElement attributeForName:@"lastUpdate"] stringValue];
        
        //Assign The Media Object
        MediaOB.MediaID = MediaID;
        MediaOB.NS_BeMediaID = NS_BeMediaID;
        MediaOB.MediaType = MediaType;
        MediaOB.WebLink = WebLink;
        MediaOB.NS_Source = NS_Source;
        MediaOB.NS_Author = NS_Author;
        MediaOB.NS_lastUpdate = NS_lastUpdate;
            
        //Create MediaText object
        NSArray *TextsElements = [mediaElement elementsForName:@"Text"];
        int text_ID = [[[[TextsElements objectAtIndex:0] attributeForName:@"ID"]stringValue]intValue];
        NSString *titleTxt = [[[[TextsElements objectAtIndex:0] attributeForName:@"Title"]stringValue] StringParseSymbol];
        NSString *descriptionTxt = [[[[TextsElements objectAtIndex:0] attributeForName:@"Description"]stringValue] StringParseSymbol];
        //Create and load the Text Object
        MediaListsText *TextOB = [[MediaListsText alloc]InitialWithtext_id:text_ID 
                                                                      title:titleTxt 
                                                                description:descriptionTxt];
        
        MediaOB.text = TextOB;
        
        
        NSArray *Videos = [mediaElement elementsForName:@"Video"];
        int video_ID = [[[[Videos objectAtIndex:0] attributeForName:@"ID"] stringValue] intValue];
        NSString *video_Type = [[[Videos objectAtIndex:0] attributeForName:@"Type"] stringValue];
        NSString *fileName = [[[[Videos objectAtIndex:0] attributeForName:@"FileName"] stringValue] StringParseSymbol];
        //Create and load the Video Object
        MediaListsVideo *VideoOB =[[MediaListsVideo alloc] video_id:video_ID
                                                                type:video_Type
                                                            filename:fileName];
        MediaOB.video = VideoOB;
        
        //Initialize the Image Array within the Object
        MediaOB.imageArray = [[NSMutableArray alloc] init];
        
        //Load into each of Image Element
        NSArray *ImageElements = [mediaElement elementsForName:@"Image"];
        
        for(GDataXMLElement *imageElement in ImageElements)
        {
            int imageID = [[[imageElement attributeForName:@"ID"] stringValue] intValue];
            NSString *imageType = [[imageElement attributeForName:@"Type"] stringValue];
            int Width = [[[imageElement attributeForName:@"Width"] stringValue] intValue];
            int Height = [[[imageElement attributeForName:@"Height"] stringValue] intValue];
            NSString *fileName = [[[imageElement attributeForName:@"FileName"] stringValue] StringParseSymbol];
            
            //Create and Load the Image Object
            MediaListsImage *ImageOB =[[MediaListsImage alloc] image_id:imageID
                                                                    type:imageType
                                                                 filename:fileName Width:Width Height:Height];
            
            //Add the ImageOB into the image Array of MediaOB
            [MediaOB.imageArray addObject:ImageOB];
        }
        
        
        //Initialize the Comment Array within the Object
        MediaOB.Array_Comment = [[NSMutableArray alloc] init];
        
        //Load into each of Comment Element
        NSArray *CommentElements = [mediaElement elementsForName:@"Comment"];
        
        for(GDataXMLElement *CommentElement in CommentElements)
        {
            int CommentID = [[[CommentElement attributeForName:@"ID"] stringValue] intValue];
            NSString *NS_Content = [[[CommentElement attributeForName:@"Content"] stringValue] StringParseSymbol];
            NSString *NS_UserName = [[CommentElement attributeForName:@"User"] stringValue];
            NSString *NS_Date = [[CommentElement attributeForName:@"lastUpdate"] stringValue];
            
            //NSString Convert NSDate.
            NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
            [formatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];
            [formatter setDateFormat: @"yyyy-MM-dd HH:mm:ss"];
            
            //Create and Load the Comment Object
            MediaListsComment *CommentOB =[[MediaListsComment alloc] init:CommentID SetContent:NS_Content SetUserName:NS_UserName SetDate:[formatter dateFromString:NS_Date]];
            //Add the CommentOB into the Comment Array of MediaOB
            [MediaOB.Array_Comment addObject:CommentOB];
        }
        
        
        //Initialize the Note Array within the Object
        MediaOB.Array_Notes = [[NSMutableArray alloc] init];
        
        //Load into each of Note Element
        NSArray *NoteElements = [mediaElement elementsForName:@"Note"];
        
        for(GDataXMLElement *NoteElement in NoteElements)
        {
            int NoteID = [[[NoteElement attributeForName:@"ID"] stringValue] intValue];
            NSString *NoteText = [[[NoteElement attributeForName:@"Text"] stringValue] StringParseSymbol];
            int AxisX = [[[NoteElement attributeForName:@"AxisX"] stringValue] intValue];
            int AxisY = [[[NoteElement attributeForName:@"AxisY"] stringValue] intValue];
            
            MediaListsNote *NoteOB = [[MediaListsNote alloc] init:NoteID SetNoteText:NoteText SetAxisX:AxisX SetAxisY:AxisY];
            
            [MediaOB.Array_Notes addObject:NoteOB];
        }
        
        
        NSArray *Array_Html = [mediaElement elementsForName:@"beHtml"];
        NSString *NS_ImageName = [[[Array_Html objectAtIndex:0] attributeForName:@"ThumbnailImage"] stringValue];
        int Width = [[[[Array_Html objectAtIndex:0] attributeForName:@"ThumbnailWidth"] stringValue] intValue];
        int Height = [[[[Array_Html objectAtIndex:0] attributeForName:@"ThumbnailHeight"] stringValue] intValue];
        NSString *NS_HTMLtitle = [[[[Array_Html objectAtIndex:0] attributeForName:@"Title"] stringValue] StringParseSymbol];
        NSString *NS_HTMLDescription = [[[[Array_Html objectAtIndex:0] attributeForName:@"Description"] stringValue] StringParseSymbol];
        
        //Create and load the Html Object
        MediaListsHtml *HtmlOB =[[MediaListsHtml alloc] InitialWithHtmlImageName:NS_ImageName SetImageWidth:Width
                                                                  SetImageHeight:Height SetHtmlTitle:NS_HTMLtitle SetHtmlDescription:NS_HTMLDescription];
        MediaOB.MLH_html = HtmlOB;
        
        //Add The MediaOB into the MediaLists
        [MediaListsOB.MediaArray addObject:MediaOB];
        
        
    }
    [MedialistsArray addObject:MediaListsOB];
    return MedialistsArray;
}

@end